package com.newlink.building.activities

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.constant.Base_RouterPath
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.utils.putSpValue
import com.newlink.building.R
import com.newlink.building.callback.Module_Phone_RegistrationCallback
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.constant.Module_Phone_Constant
import com.newlink.building.databinding.AccountLoginActivityBinding
import com.newlink.building.manager.Module_Phone_VoipController

/**
 * Created by xiaodong on 2022/7/28.
 */
@Route(path = Base_RouterPath.PATH_ACCOUNTLOGIN)
class Module_Phone_AccountLoginActivityNL : NL_BaseActivity(), View.OnClickListener {

    private lateinit var registrationCallback: Module_Phone_RegistrationCallback
    private lateinit var mBinding: AccountLoginActivityBinding
    override fun attachLayoutRes(): View {
        mBinding = AccountLoginActivityBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {

    }

    override fun initView() {
        val authInfo = Module_Phone_VoipController.get().coreContext.core.defaultAccount?.findAuthInfo()
        if (authInfo != null) {
            mBinding.username.setText(authInfo.username)
            mBinding.password.setText("1234")
            mBinding.domain.setText(authInfo.domain)
            val realmTxt = if (authInfo.realm != authInfo.domain) authInfo.realm else ""
            mBinding.realm.setText(realmTxt)
            mBinding.port.setText("7060")
        }
        if (Module_Phone_VoipController.get().isOnLine) {
            mBinding.connect.isEnabled = false
            mBinding.disconnect.isEnabled = true
            mBinding.delete.isEnabled = true
        }
        mBinding.coreVersion.text = Module_Phone_VoipController.get().coreContext.core.version

        initListener()
    }

    fun initListener() {
        mBinding.connect.setOnClickListener(this)
        mBinding.disconnect.setOnClickListener(this)
        mBinding.delete.setOnClickListener(this)

        registrationCallback = object : Module_Phone_RegistrationCallback {
            override fun registrationNone() {
            }

            override fun registrationProgress() {
            }

            override fun registrationOk() {
                logDebug<Module_Phone_AccountLoginActivityNL>("registrationOk")
                showToast("账号注册成功")
                mBinding.connect.isEnabled = false
                mBinding.disconnect.isEnabled = true
                mBinding.delete.isEnabled = true
                putSpValue(Module_Phone_Constant.DOMAIN_ADDRESS, mBinding.domain.text.toString())
            }

            override fun registrationCleared() {
                logDebug<Module_Phone_AccountLoginActivityNL>("registrationCleared")
                mBinding.connect.isEnabled = true
                mBinding.disconnect.isEnabled = false
                mBinding.delete.isEnabled = false
            }

            override fun registrationFailed() {
                logDebug<Module_Phone_AccountLoginActivityNL>("registrationFailed")
                showToast("账号注册失败")
                mBinding.connect.isEnabled = true
                mBinding.disconnect.isEnabled = false
                mBinding.delete.isEnabled = false
            }
        }

        Module_Phone_VoipController.get().setRegisterCallback(registrationCallback)
    }

    private fun delete() {
        Module_Phone_VoipController.get().delete()
    }

    private fun unregister() {
        Module_Phone_VoipController.get().unregister()
    }

    private fun login() {
        Module_Phone_VoipController.get().registerAccount(
            mBinding.username.text.toString(),
            mBinding.password.text.toString(),
            mBinding.domain.text.toString(),
            mBinding.port.text.toString(),
            mBinding.realm.text.toString()
        )
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.connect -> {
                login()
            }

            R.id.disconnect -> {
                unregister()
            }

            R.id.delete -> {
                delete()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (registrationCallback != null) {
            Module_Phone_VoipController.get().unRegisterCallback(registrationCallback)
        }
    }
}
